home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14464 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: #include "" for large programs.
  5. Date: 14 Apr 1996 22:39:57 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4krurt$fat@sparcserver.lrz-muenchen.de>
  9. References: <Pine.SUN.3.92.960411195730.24973A-100000@suntan> <4krrmi$7ef@mother.usf.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. gohel@csee.usf.edu (Himanshu Gohel) writes:
  13.  
  14. >In article <Pine.SUN.3.92.960411195730.24973A-100000@suntan>, "Carlos Diaz (CS)" <cdiaz@eng.usf.edu> writes:
  15.  
  16. >The way to avoid multiple definitions (and similarly multiple inclusions
  17. >of .h files) is to do the following:
  18.  
  19. >#ifndef __MYHEADERFILE__
  20. >#define __MYHEADERFILE__
  21. > ...all your header file stuff here...
  22. >#endif
  23.  
  24. Change this to something not using macro names starting with a 
  25. double underscore and an upper case letter and I will happily
  26. agree with you.
  27.  
  28. >Now you can include this .h file as many times as you want and you
  29. >won't have problems with multiple defintions because of the #ifndef.
  30.  
  31. There also is a non zero chance that the stuff defined in the 
  32. header file will be underfined in your compilation module that
  33. includes such a header.
  34.  
  35. >Also, there is a place for the const keyword, but in C that cannot
  36. >be put in a .h file.  It has to go in a C file.
  37.  
  38. Try something like
  39.  
  40.    #if defined(GLOBALS)
  41.    #define GLOBAL
  42.    #else
  43.    #define GLOBAL extern
  44.    #endif
  45.  
  46.    GLOBAL const answer
  47.    #if defined(GLOBALS)
  48.      = 42;
  49.    #else
  50.      ;
  51.    #endif
  52.  
  53. Be sure to define "GLOBALS" in exactly one compilation unit that includes
  54. a header file with that construct.
  55.  
  56. Kurt
  57. -- 
  58. | Kurt Watzka                             Phone : +49-89-2180-6254
  59. | watzka@stat.uni-muenchen.de
  60.